﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///PSDZB 的摘要说明
/// </summary>
public partial class PSDZB
{
    public enum type { 普通用户 = 1, 机构收获地址, 个人收获地址 };
    public static PSDZB GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.PSDZB.FirstOrDefault(row => row.ID == id && row.Enable_Flag == 1);
        }
    }

    public static PSDZB GetIsRankByUserID(int Userid)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.PSDZB.FirstOrDefault(row => row.AddUserID == Userid && row.IsRank == true && row.Enable_Flag == 1);
        }
    }



    public static List<PSDZB> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.PSDZB.ToList();
        }
    }
    public static List<PSDZB> GetAllByAddUserID(int AddUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.PSDZB.Where(row => row.AddUserID == AddUserID && row.Enable_Flag == 1).ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.PSDZB.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<PSDZB>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.PSDZB.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.PSDZB.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<PSDZB> GetAllByConditon(int sheng, int shi, int qu, string name, int typeUser, string telPhone, string phone, string address, int userID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.PSDZB
                        where t.Enable_Flag == 1
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(row => row.UserName.Contains(name));
            if (typeUser != 0)
                query = query.Where(row => row.Type == typeUser);
            if (!string.IsNullOrEmpty(telPhone))
                query = query.Where(row => row.Telphone.Contains(telPhone));
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(row => row.Phone.Contains(phone));
            if (!string.IsNullOrEmpty(address))
                query = query.Where(row => row.Address.Contains(address));
            if (userID != 0)
                query = query.Where(row => row.AddUserID == userID);
         
            return query.ToList();
        }
    }
    public static List<PSDZB> GetAllInUserIDByConditon(int sheng, int shi, int qu, string name, int typeUser, string telPhone, string phone, string address, int userID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.PSDZB
                        where t.Enable_Flag == 1 && t.AddUserID == userID
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(row => row.UserName.Contains(name));
            if (typeUser != 0)
                query = query.Where(row => row.Type == typeUser);
            if (!string.IsNullOrEmpty(telPhone))
                query = query.Where(row => row.Telphone.Contains(telPhone));
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(row => row.Phone.Contains(phone));
            if (!string.IsNullOrEmpty(address))
                query = query.Where(row => row.Address.Contains(address));
          

            return query.ToList();
        }
    }
    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            PSDZB objWGXX = PSDZB.GetByID(ID);
            if (objWGXX == null)
                return;
            objWGXX.Enable_Flag = 0;
            objWGXX.Update();
        }
    }
}